package com.javasm.mingming.webuser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.javasm.mingming.common.RedisKeys;
import com.javasm.mingming.webuser.dao.WebUserDao;
import com.javasm.mingming.webuser.entity.WebUser;
import com.javasm.mingming.webuser.service.WebUserInfoService;
import com.javasm.mingming.webuser.service.WebUserService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * (WebUser)表服务实现类
 *
 * @author makejava
 * @since 2025-01-06 14:46:09
 */
@Service("webUserService")
public class WebUserServiceImpl extends ServiceImpl<WebUserDao, WebUser> implements WebUserService {

    @Resource
    WebUserInfoService webUserInfoService;

    @Resource
    RedisTemplate<String,Object> redisTemplate;
    @Override
    public WebUser getByUname(String username) {
        //当前的方法,只有一个责任
/*        LambdaQueryWrapper<WebUser> queryWrapper  = new LambdaQueryWrapper<>();
        queryWrapper.eq(WebUser::getUsername,username);
        WebUser webUser = getOne(queryWrapper);
        //查询 用户详情
        if (webUser != null && webUser.getUid() != null){
            WebUserInfo userInfo = webUserInfoService.getById(webUser.getUid());
            webUser.setUserInfo(userInfo);
        }*/
        String key = String.format(RedisKeys.User_Uname,username);
        Object o = redisTemplate.opsForValue().get(key);
        if (o != null){
            return (WebUser) o;
        }
        return null;
    }

    @Override
    public WebUser getByUid(Integer id) {
        String key = String.format(RedisKeys.User_Uid,id);
        Object o = redisTemplate.opsForValue().get(key);
        if (o != null){
            return (WebUser) o;
        }
        return null;
    }

    @Override
    public WebUser getEmail(String mail) {
        LambdaQueryWrapper<WebUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WebUser::getEmail,mail);
        WebUser webUser = getOne(queryWrapper);
        return webUser;
    }
}

